package com.cn.web.controller.Equipment;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.cn.bean.dto.ComponentGroupDto;
import com.cn.bean.dto.SingleRegistrationTableDto;
import com.cn.bean.pojo.lbj.*;
import com.cn.bean.vo.Equipment.ComponentBasicInformation;
import com.cn.config.config.Result;
import com.cn.service.server.Equipment.ComponentGroupService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@CrossOrigin
@RestController
@Slf4j
@Transactional
public class ComponentGroupController {
    @Autowired
    private ComponentGroupService componentGroupService;
    @RequestMapping("/select/ComponentGroupBasicMainTable")
    @ResponseBody
    public List<ComponentGroupBasicMainTable> selectComponentGroupBasicMainTable(@RequestBody ComponentGroupBasicMainTable componentGroupBasicMainTable) {
        List<ComponentGroupBasicMainTable> assists = componentGroupService.selectComponentBasic(componentGroupBasicMainTable);
        return assists;
    }
    @RequestMapping("/select/ComponentGroupBasicSubTable")
    @ResponseBody
    public List<ComponentGroupSettingSubTable> selectComponentGroupBasicSubTable(@RequestBody ComponentGroupSettingSubTable subTable) {
        List<ComponentGroupSettingSubTable> subTables = componentGroupService.selectComponentGroupSub(subTable);
        return subTables;
    }
    @RequestMapping("/select/ComponentBasic")
    @ResponseBody
    public List<ComponentBasicInformationTable> selectComponentBasic(@RequestBody ComponentBasicInformationTable componentBasicInformationTable) {
        List<ComponentBasicInformationTable> subTables = componentGroupService.selectComponentBasicInformationTable(componentBasicInformationTable);
        return subTables;
    }
    @RequestMapping("/select/Single")
    @ResponseBody
    public List<SingleRegistrationTable> selectSingle(@RequestBody SingleRegistrationTable singleRegistrationTable) {
        List<SingleRegistrationTable> subTables = componentGroupService.selectMain(singleRegistrationTable);
        return subTables;
    }
    @RequestMapping("/select/SingleSub")
    @ResponseBody
    public List<SingleRegistrationTableSub> selectSingleSub(@RequestBody SingleRegistrationTableSub sub) {
        List<SingleRegistrationTableSub> subTables = componentGroupService.selectSub(sub);
        return subTables;
    }
    @RequestMapping("/select/SingSub")
    @ResponseBody
    public List<SingleRegistrationTableSub> selectSingSub(@RequestBody SingleRegistrationTableSub sub) {
        List<SingleRegistrationTableSub> subTables = componentGroupService.selectSingleSub(sub);
        return subTables;
    }
    @RequestMapping("/insert/ComponentGroup")
    @ResponseBody
    public Result insertComponentGroup(@RequestBody JSONObject jsonObject) {
        JSONArray dataArray = jsonObject.getJSONArray("requestData");
        ComponentGroupSettingMainTable settingMainTable = new ComponentGroupSettingMainTable();
        ComponentGroupBasicMainTable basicMainTable = new ComponentGroupBasicMainTable();
        ComponentGroupUpdateMainTable updateMainTable = new ComponentGroupUpdateMainTable();
        Integer id = componentGroupService.getComponentGroupSettingAutoid();
        Date date=new Date();
        SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        id = id != null ? ++id : 1;
        String userName = jsonObject.getString("userName");
        int equipmentStructureId=jsonObject.getInteger("equipmentStructureId");
        int componentId=jsonObject.getInteger("componentId");
        int componentQuantity=jsonObject.getInteger("componentQuantity");
        int spacerQuantity=jsonObject.getInteger("spacerQuantity");
        String spacer=jsonObject.getString("spacer");
        String specialSpacer=jsonObject.getString("specialSpacer");
        String groupNumber=jsonObject.getString("groupNumber");
        String rollBoxCode=jsonObject.getString("rollBoxCode");
        String groupingDevice=jsonObject.getString("groupingDevice");
        String specification=jsonObject.getString("specification");
        int categoryOfConsumables=jsonObject.getInteger("categoryOfConsumables");
        settingMainTable.setId(id);
        settingMainTable.setEquipmentStructureId(equipmentStructureId);
        settingMainTable.setComponentId(componentId);
        settingMainTable.setComponentQuantity(componentQuantity);
        settingMainTable.setSpacerQuantity(spacerQuantity);
        settingMainTable.setSpacer(spacer);
        settingMainTable.setSpecialSpacer(specialSpacer);
        settingMainTable.setGroupNumber(groupNumber);
        settingMainTable.setRollBoxCode(rollBoxCode);
        settingMainTable.setGroupingDevice(groupingDevice);
        settingMainTable.setDjNumber(String.valueOf(id));
        settingMainTable.setCategoryOfConsumables(categoryOfConsumables);
        settingMainTable.setDjStatus(0);
        settingMainTable.setSpecification(specification);
        settingMainTable.setCjr(userName);
        settingMainTable.setCjsj(format.format(date));
        BeanUtils.copyProperties(settingMainTable, basicMainTable);
        BeanUtils.copyProperties(settingMainTable, updateMainTable);
        basicMainTable.setGroupId(id);
        updateMainTable.setCjr(userName);
        updateMainTable.setCjsj(format.format(date));
        int a = componentGroupService.insertComponentGroupSetting(settingMainTable);
        int b=componentGroupService.insertComponentGroupUpdate(updateMainTable);
        int c=componentGroupService.insertComponentGroupBasicMain(basicMainTable);
        if (a != 1&&b!=1&&c!=1) {
            return Result.failure("500");
        }
        if (dataArray != null && !dataArray.isEmpty()) {
            List<ComponentGroupDto> djBhList = dataArray.toJavaList(ComponentGroupDto.class);
            for (ComponentGroupDto dataStructureDto : djBhList) {
                ComponentGroupBasicSubTable basicSubTable = new ComponentGroupBasicSubTable();
                ComponentGroupSettingSubTable settingSubTable=new ComponentGroupSettingSubTable();
                ComponentGroupUpdateSubTable updateSubTable = new ComponentGroupUpdateSubTable();
                Integer autoid = componentGroupService.getComponentGroupSettingSubAutoid();
                autoid = autoid != null ? ++autoid : 1;
                BeanUtils.copyProperties(dataStructureDto, basicSubTable);
                BeanUtils.copyProperties(dataStructureDto, settingSubTable);
                BeanUtils.copyProperties(dataStructureDto, updateSubTable);
                settingSubTable.setId(autoid);
                settingSubTable.setMid(id);
                updateSubTable.setId(autoid);
                updateSubTable.setMid(id);
                basicSubTable.setId(autoid);
                basicSubTable.setMid(id);
                int d = componentGroupService.insertComponentGroupSettingSub(settingSubTable);
                int e = componentGroupService.insertComponentGroupUpdateSub(updateSubTable);
                int f=componentGroupService.insertComponentGroupBasicSub(basicSubTable);
//                int c = componentSingleRegistrationService.insertHistoryDataTable(historyTable);
                if (d != 1&&e != 1&&f != 1) {
                    return Result.failure("500");
                }
            }
        }

        return Result.success(200);
    }
    @RequestMapping("/update/ComponentGroup")
    @ResponseBody
    public Result updateComponentGroup(@RequestBody JSONObject jsonObject) {
        JSONArray dataArray = jsonObject.getJSONArray("requestData");
//        log.debug("dataStructureDto.getChineseName="+dataStructureDto.getChineseName());
        ComponentGroupSettingMainTable settingMainTable = new ComponentGroupSettingMainTable();
        ComponentGroupBasicMainTable basicMainTable = new ComponentGroupBasicMainTable();
        ComponentGroupUpdateMainTable updateMainTable = new ComponentGroupUpdateMainTable();
        Date date=new Date();
        SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        int id = jsonObject.getInteger("id");
        String userName = jsonObject.getString("userName");
        int equipmentStructureId=jsonObject.getInteger("equipmentStructureId");
        int componentId=jsonObject.getInteger("componentId");
        int componentQuantity=jsonObject.getInteger("componentQuantity");
        int spacerQuantity=jsonObject.getInteger("spacerQuantity");
        String spacer=jsonObject.getString("spacer");
        String specialSpacer=jsonObject.getString("specialSpacer");
        String groupNumber=jsonObject.getString("groupNumber");
        String rollBoxCode=jsonObject.getString("rollBoxCode");
        String groupingDevice=jsonObject.getString("groupingDevice");
        String specification=jsonObject.getString("specification");
        int categoryOfConsumables=jsonObject.getInteger("categoryOfConsumables");
        int groupingStatus=jsonObject.getInteger("groupingStatus");
        settingMainTable.setId(id);
        settingMainTable.setEquipmentStructureId(equipmentStructureId);
        settingMainTable.setComponentId(componentId);
        settingMainTable.setComponentQuantity(componentQuantity);
        settingMainTable.setSpacerQuantity(spacerQuantity);
        settingMainTable.setSpacer(spacer);
        settingMainTable.setSpecialSpacer(specialSpacer);
        settingMainTable.setGroupNumber(groupNumber);
        settingMainTable.setRollBoxCode(rollBoxCode);
        settingMainTable.setGroupingDevice(groupingDevice);
        settingMainTable.setDjNumber(String.valueOf(id));
        settingMainTable.setCategoryOfConsumables(categoryOfConsumables);
        settingMainTable.setDjStatus(0);
        settingMainTable.setSpecification(specification);
        BeanUtils.copyProperties(settingMainTable, basicMainTable);
        BeanUtils.copyProperties(settingMainTable, updateMainTable);
        basicMainTable.setGroupId(id);
        basicMainTable.setStatus(groupingStatus);
        updateMainTable.setXgr(userName);
        updateMainTable.setXgsj(format.format(date));
        int a =componentGroupService.updateComponentGroupSetting(settingMainTable);
        int b=componentGroupService.updateComponentGroupUpdate(updateMainTable);
        int c=componentGroupService.updateComponentGroupBasic(basicMainTable);
        if (a != 1&&b!=1&&c!=1) {
            return Result.failure("500");
        }
        List<ComponentGroupBasicSubTable> statusOperationDtos=componentGroupService.selectBasicSub(id);
        if (statusOperationDtos.size()>0){
            int sub = componentGroupService.delComponentGroupBasicSub(id);
            int aub = componentGroupService.delComponentGroupSettingSub(id);
            int cub = componentGroupService.delComponentGroupUpdateSub(id);
        }
        if (dataArray != null && !dataArray.isEmpty()) {
            List<ComponentGroupDto> djBhList = dataArray.toJavaList(ComponentGroupDto.class);
            for (ComponentGroupDto dataStructureDto : djBhList) {
                ComponentGroupBasicSubTable basicSubTable = new ComponentGroupBasicSubTable();
                ComponentGroupSettingSubTable settingSubTable=new ComponentGroupSettingSubTable();
                ComponentGroupUpdateSubTable updateSubTable = new ComponentGroupUpdateSubTable();
                Integer autoid = componentGroupService.getComponentGroupSettingSubAutoid();
                autoid = autoid != null ? ++autoid : 1;
                BeanUtils.copyProperties(dataStructureDto, basicSubTable);
                BeanUtils.copyProperties(dataStructureDto, settingSubTable);
                BeanUtils.copyProperties(dataStructureDto, updateSubTable);
                settingSubTable.setId(autoid);
                settingSubTable.setMid(id);
                updateSubTable.setId(autoid);
                updateSubTable.setMid(id);
                basicSubTable.setId(autoid);
                basicSubTable.setMid(id);
                int d = componentGroupService.insertComponentGroupSettingSub(settingSubTable);
                int e = componentGroupService.insertComponentGroupUpdateSub(updateSubTable);
                int f=componentGroupService.insertComponentGroupBasicSub(basicSubTable);
                if (d != 1&&e != 1&&f != 1) {
                    return Result.failure("500");
                }
            }
        }

        return Result.success(200);
    }
    @RequestMapping("/update/GroupStatus")
    @ResponseBody
    public Result updateGroupStatus(@RequestBody JSONObject jsonObject){
        JSONArray djBhArray = jsonObject.getJSONArray("id");
        String userName = jsonObject.getString("userName");
        Date date=new Date();
        SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        if (djBhArray != null && !djBhArray.isEmpty()) {
            List<Integer> djBhList = djBhArray.toJavaList(Integer.class);
            for (Integer id : djBhList) {
                componentGroupService.updateGroupStatus(id);
            }
            return Result.success(200);
        }
        return Result.failure("500", "审核失败");
    }
    @RequestMapping("/update/updateSettingStatus")
    @ResponseBody
    public Result updateSettingStatus(@RequestBody JSONObject jsonObject){
        JSONArray djBhArray = jsonObject.getJSONArray("id");
        String userName = jsonObject.getString("userName");
        Date date=new Date();
        SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        if (djBhArray != null && !djBhArray.isEmpty()) {
            List<Integer> djBhList = djBhArray.toJavaList(Integer.class);
            for (Integer id : djBhList) {
                componentGroupService.updateSettingStatus(id);
            }
            return Result.success(200);
        }
        return Result.failure("500", "审核失败");
    }
}
